var util = require('../../libs/util.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iot_gps_brand:['策动智冷盒','易流','G7','车联网','超越（JT808）','深圳点创'],
    iot_gps_brand_index:0
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": "车辆档案" });
    var that = this;
    //更新数据
    that.setData({
      vehicle_id: e.id,
      tab: e.tab || 0,
      role_default:app.globalData.role_default
    });
    that.loadData();
  },
  loadData: function () {
    var that = this;
    wx.showLoading({
      title: '请稍候...',
    });
    util.getData("/wechat/vehicle/iotBox", { vehicle_id: that.data.vehicle_id }, function (res) {
      console.log(res.data.data)
      if (res.data.success) {
        that.setData(res.data.data)
        that.setData({
          plate: that.data.plate2||''
        });
        if(that.data.brand == 'y6'){
          that.setData({
            iot_gps_brand_index: 1
          });
        }
        if(that.data.brand == 'g7'){
          that.setData({
            iot_gps_brand_index: 2
          });
        }
        if(that.data.brand == 'clw'){
          that.setData({
            iot_gps_brand_index: 3
          });
        }
        if(that.data.brand == 'szdc'){
          that.setData({
            iot_gps_brand_index: 5
          });
        }
        wx.setNavigationBarTitle({ "title": "车辆档案--" + that.data.plate });
      }
      wx.hideLoading();
    });
  },
  useWX:function(){
    this.setData({
      iot_box:'WX'+this.data.vehicle_id
    })
  },
  loadPublicConfig:function(){
    var that = this;
    wx.showLoading({
      title: '请稍候...',
    });
    util.getData("/wechat/vehicle/iotBox_publicconfig", { vehicle_id: that.data.vehicle_id,brand:that.data.brand }, function (res) {
      console.log(res.data.data)
      if (res.data.success) {
        that.setData(res.data.data)
      }
      wx.hideLoading();
    });
  },
  submitForms: function (e) {
    var that = this;
    if (!that.data.plate) {
      wx.showToast({
        title: "请输入车牌",
        image: '/images/wrong.png',
        duration: 2000
      });
      return false;
    }
    if (!that.data.iot_box && that.data.iot_gps_brand_index == 0) {
      wx.showToast({
        title: "请输入盒子编号",
        image: '/images/wrong.png',
        duration: 2000
      });
      return false;
    }
    if (!that.data.appkey && that.data.iot_gps_brand_index == 1) {
      wx.showToast({
        title: "请输入公钥",
        image: '/images/wrong.png',
        duration: 2000
      });
      return false;
    }
    if (!that.data.appsecret && that.data.iot_gps_brand_index == 1) {
      wx.showToast({
        title: "请输入密钥",
        image: '/images/wrong.png',
        duration: 2000
      });
      return false;
    }
    if (!that.data.iot_box && that.data.iot_gps_brand_index == 3) {
      wx.showToast({
        title: "请输入设备号",
        image: '/images/wrong.png',
        duration: 2000
      });
      return false;
    }
    if (!that.data.iot_box && that.data.iot_gps_brand_index == 5) {
      wx.showToast({
        title: "请输入设备号",
        image: '/images/wrong.png',
        duration: 2000
      });
      return false;
    }
    var brand = 'cd';
    if (that.data.iot_gps_brand_index == 1){
      brand = 'y6';
    }
    if (that.data.iot_gps_brand_index == 2){
      brand = 'g7';
    }
    if (that.data.iot_gps_brand_index == 3){
      brand = 'clw';
    }
    if (that.data.iot_gps_brand_index == 4){
      brand = 'jt808';
    }
    if (that.data.iot_gps_brand_index == 5){
      brand = 'szdc';
    }
    wx.showLoading({
      title: '请稍候...',
    });
    if (that.data.ID){
      util.putData("/wechat/vehicle/iotBox", {
        id: that.data.ID,
        plate: that.data.plate,
        vehicle: that.data.vehicle_id,
        iot_box: that.data.iot_box || '',
        appkey: that.data.appkey || '',
        appsecret: that.data.appsecret || '',
        user: that.data.user || '',
        password: that.data.password || '',
        brand: brand,
      }, function (res, error) {
        console.log(res); console.log(error)
        wx.hideLoading();
        if (error) {
          wx.showToast({
            title: res.data.data,
            image: '/images/wrong.png',
            duration: 3000
          });
          return false;
        }
        if (res.data.success) {
          wx.showToast({
            title: res.data.data,
            icon: "success",
            duration: 2000,
            success:function(){
              setTimeout(function(){
                that.loadData();
              },2000)
            }
          });
        } else if (res.data.success == false) {
          wx.showModal({
            title: '提交失败',
            content: res.data.data,
            confirmText: '确认',
            cancelText: '取消',
            success(res) {
              if (res.confirm) {
                
              }
            }
          })
        }
      });
    }else{
      util.postData("/wechat/vehicle/iotBox", {
        plate: that.data.plate,
        vehicle:that.data.vehicle_id,
        iot_box: that.data.iot_box || '',
        appkey: that.data.appkey || '',
        appsecret: that.data.appsecret || '',
        user: that.data.user || '',
        password: that.data.password || '',
        brand: brand,
      }, function (res, error) {
        console.log(res); console.log(error)
        wx.hideLoading();
        if (error) {
          wx.showToast({
            title: res.data.data,
            image: '/images/wrong.png',
            duration: 3000
          });
          return false;
        }
        if (res.data.success) {
          wx.showToast({
            title: res.data.data,
            icon: "success",
            duration: 2000,
            success: function () {
              setTimeout(function () {
                that.loadData();
              }, 2000)
            }
          });
        } else if (res.data.success == false) {
          wx.showToast({
            title: res.data.data,
            image: '/images/wrong.png',
            duration: 3000,
            mask: true
          });
        }
      });
    }
    
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  iotGpsBrandChange: function (e) {
    var brand = 'cd';
    if (e.detail.value == 1){
      brand = 'y6';
    }
    if (e.detail.value == 2){
      brand = 'g7';
    }
    if (e.detail.value == 3){
      brand = 'clw';
    }
    if (e.detail.value == 4){
      brand = 'jt808';
    }
    if (e.detail.value == 5){
      brand = 'szdc';
    }
    this.setData({
      iot_gps_brand_index: e.detail.value,
      brand:brand
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})